package com.amway.qy.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amway.qy.core.constants.QyWechatUrls;
import com.amway.qy.core.service.QyAccessTokenService;
import com.amway.qy.core.service.QySecondAuthenticationService;
import com.amway.qy.resource.result.BaseResult;
import com.amway.qy.resource.result.auth.QySecondAuthResult;
import com.amway.qy.util.common.QyHttpClientUtils;
import com.google.gson.Gson;

public class QySecondAuthenticationServiceImpl implements QySecondAuthenticationService {
	private static final Logger logger = LoggerFactory.getLogger(QySecondAuthenticationServiceImpl.class);
	
	private QyAccessTokenService accessTokenManager;
	
	public QySecondAuthenticationServiceImpl(QyAccessTokenService accessTokenManager){
		this.accessTokenManager = accessTokenManager;
	}

	@Override
	public String secondAuthSuccessInRaw(String userId) {
		logger.debug("secondAnthSuccess --> userId -->"+userId);
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("userid",userId));
		return QyHttpClientUtils.httpsGet(QyWechatUrls.SECOND_AUTH_SUCCESS,paramsMap);
	}

	@Override
	public BaseResult secondAuthSuccess(String userId) {
		return new Gson().fromJson(secondAuthSuccessInRaw(userId), BaseResult.class);
	}

	@Override
	public QySecondAuthResult getMemberIdByCode(String code) {
		 
		return new Gson().fromJson(getMemberIdByCodeInRaw(code), QySecondAuthResult.class);
	}

	/*
	 * 根据code获取成员信息
	 * 主要是根据Code获取用户的userId
	 * 
	 * @Return:
	 * a)企业成员授权时返回示例如下：

		{
		   "UserId":"USERID",
		   "DeviceId":"DEVICEID"
		}
	 * 
	 * b)非企业成员授权时返回示例如下：

		{
		   "OpenId":"OPENID",
		   "DeviceId":"DEVICEID"
		}
	 */
	
	@Override
	public String getMemberIdByCodeInRaw(String code) {
		logger.debug("getMemberIdByCodeInRaw --> OAUTH Code -->"+code);
		
		List<NameValuePair> paramsMap = new ArrayList<NameValuePair>();
		paramsMap.add(new BasicNameValuePair("access_token", accessTokenManager.getAccessToken()));
		paramsMap.add(new BasicNameValuePair("code",code));
		return QyHttpClientUtils.httpsGet(QyWechatUrls.GET_MEMBER_ID__BY_CODE,paramsMap);
	}
	
	

	//----------------------Getter And Setter--------------------------------------
	public QyAccessTokenService getAccessTokenManager() {
		return accessTokenManager;
	}

	public void setAccessTokenManager(QyAccessTokenService accessTokenManager) {
		this.accessTokenManager = accessTokenManager;
	}

	@Override
	public QyAccessTokenService getQyAccessTokenService() {
		// TODO Auto-generated method stub
		return accessTokenManager;
	}

	@Override
	public void setQyAccessTokenService(QyAccessTokenService tokenService) {
		this.accessTokenManager = tokenService;
	}
	 

}
